package code.module.motion
{
	import code.main.controller.event.EventManager;
	import code.main.controller.event.EventName;
	import code.main.controller.event.MyEvent;
	
	import com.yy.dataStructure.HashMap;
	import com.yy.manager.ResourceManager;
	import com.yy.net.event.EventCenter;
	import com.yy.tools.arry.ArrayTools;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class MotionDm
	{
		private static var _instance:MotionDm;
		private var _motionMap:HashMap;
		/**7个玩家 播放动画位置**/
		private var _posArr:Array = [
			[606,97],
			[694,264],
			[606,425],
			[386,487],
			[158,425],
			[63,264],
			[156,97],
		]; 
		private var _emotionIds:Array = [
			[1],[2],[3],[4],[5],
			[6],[7],[8],[9],[10],
			[11],[12],[13],[14],[15],
			[16],[17],[18],[19],[20],
			[21],[22],[23],[24],[25],
			[26],
		];
		public function MotionDm(xx:XX)
		{
			init();
		}
		public static function getInstance():MotionDm {
			if(!_instance) {
				_instance = new MotionDm(new XX());
			}
			return _instance;
		}
		
		/**
		 *初始化表情 
		 * 
		 */
		private function init():void {
			_motionMap = new HashMap();
			var MotionClass:Class;
			var arr:Array = this._emotionIds;
			var len:int = arr.length;
			var i:int;
			var vo:MotionVo;
			//普通表情
			for(var j:int=0; j<len; j++) {
				i = arr[j][0];
				vo = new MotionVo();
				vo.motionId = i;
				//玩家看见的动画
				MotionClass = ResourceManager.getClassByName("expression.MCute" + i);
				vo.showMotionClass = MotionClass;
				//预览的动画
				vo.preShowMotion = ResourceManager.getMovieClipByName("expression.Cute" + i);
				vo.preShowMotion.gotoAndStop(2);
				//pic
				vo.motionPic = ResourceManager.getMovieClipByName("expression.button.Cute" + i);
				vo.motionPic.buttonMode = true;
				vo.motionPic.mouseChildren = false;
				vo.motionPic.width = 36;
				vo.motionPic.height = 36;
				vo.motionPic.id = vo.motionId;
				vo.motionPic.addEventListener(MouseEvent.ROLL_OVER, overMotionFunc);
				vo.motionPic.addEventListener(MouseEvent.ROLL_OUT, outMotionFunc);
				vo.motionPic.addEventListener(MouseEvent.CLICK, clickMotionFunc);
				_motionMap.put(vo.motionId, vo);
			}
		}
		
		/**
		 *鼠标移动到表情上 
		 * @param e
		 * 
		 */
		private function overMotionFunc(e:Event):void {
			var me:MyEvent = new MyEvent(EventName.MOTION_OVER_MOTION);
			me.intData = e.currentTarget.id;
			EventManager.getInstance().dispatchEvent(me);
		} 
		/**
		 *鼠标移开表情
		 * @param e
		 * 
		 */
		private function outMotionFunc(e:Event):void {
			var me:MyEvent = new MyEvent(EventName.MOTION_OUT_MOTION);
			me.intData = e.currentTarget.id;
			EventManager.getInstance().dispatchEvent(me);
		} 
		/**
		 *点击表情按钮
		 * @param e
		 * 
		 */
		private function clickMotionFunc(e:Event):void {
			var me:MyEvent = new MyEvent(EventName.MOTION_CLICK_MOTION);
			me.intData = e.currentTarget.id;
			EventManager.getInstance().dispatchEvent(me);
		} 
		
		/**
		 *获得表情列表 
		 * @return 
		 * 
		 */
		public function getMotionList():Array{
			return _motionMap.values();
		}
		
		/**
		 *获得一页表情列表 
		 * @param page
		 * @param pageSize
		 * 
		 */
		public function getPageList(page:int, pageSize:int):Array {
			var list:Array = getMotionList();
			return ArrayTools.runPage(list, page, pageSize);
		}
		
		/**
		 * 获得表情vo
		 * @param id
		 * @return 
		 * 
		 */
		public function getMotionVoById(id:int):MotionVo {
			return _motionMap.getValue(id);
		}
		
		/**
		 *获得表情预览动画 
		 * @param id
		 * @return 
		 * 
		 */
		public function getPreMotionById(id:int):MovieClip {
			var vo:MotionVo = getMotionVoById(id); 
			if(vo) {
				return vo.preShowMotion;
			}
			return null;
		}
		
		/**
		 *获得表情预览pic
		 * @param id
		 * @return 
		 * 
		 */
		public function getMotionPicById(id:int):MovieClip {
			var vo:MotionVo = getMotionVoById(id); 
			if(vo) {
				return vo.motionPic;
			}
			return null;
		}
		/**
		 *获得表情预览动画
		 * @param id
		 * @return 
		 * 
		 */
		public function getShowMotionById(id:int):MovieClip {
			var vo:MotionVo = getMotionVoById(id); 
			if(vo) {
				return new vo.showMotionClass();
			}
			return null;
		}
		
		/**
		 *通过seatId获取播放表情的位置 
		 * @param id
		 * @return 
		 * 
		 */
		public function getMotionPosBySeatId(seatId:int):Point {
			var arr:Array = _posArr[seatId-1];
			return new Point(arr[0], arr[1]);
		}
	}
}
class XX{}